package org.example.Thread;


public class Print {
    public static void main(String[] args) throws InterruptedException{
        Thread1 th1 = new Thread1("a");
        Thread1 th2 = new Thread1("b");
        Thread t1 = new Thread(th1);
        Thread t2 = new Thread(th2);
        t1.start();
        t2.start();
    }
}
class Thread1 implements Runnable{
    volatile static int x = 0;//用于同步多个线程，volatile用于解决读写屏障，static用于确保线程之间使用的变量是同一个
    String name;
    Thread1(){};
    Thread1(String a){
        this.name = a;
    }
    @Override
    public void run() {
        System.out.println(name + "线程进入");
        for (int i = 1; i <= 10; i++) {
            while(x == 1&&name.equals("a")) ;//缺陷，此处有资源浪费
            while(x == 0&&name.equals("b")) ;
            System.out.println(i + " " + name+" " + x);
            x^=1;
        }
    }
}